package com.fingard.dsp.bank.directbank.bocom01;

import com.bocom.jump.bp.util.Base64;
import com.fingard.dsp.bank.directbank.bankInterface.IInCustodyInfo;
import com.fingard.dsp.bank.directbank.fngrd01.util.StringHelper;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.ReqQryInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.RetAtsInCustodyInfo;
import com.fingard.dsp.bank.reqrespobj.qryincustodyinfo.RetAtsInCustodyInfoDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 获取在押车辆信息
 * 查询有三个规则：1、直接送三方协议；2、经销商证件号和类型+主机厂证件号和类型；3、特约店代码+主机厂证件号和类型
 *
 * @author marlon
 * @date 2019/1/15
 */
public class BOCOM01QryInCustodyInfo extends BOCOM01Base implements IInCustodyInfo {


    @Override
    public RetAtsInCustodyInfo qryInCustodyInfo(ReqQryInCustodyInfo recInfo) throws Exception {
        RetAtsInCustodyInfo retAts = new RetAtsInCustodyInfo(recInfo.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            String page = "0";
            int index = 0;
            //分页查询
            while ("0".equals(page)) {
                tr_code = "queryInCustodyCertsInfo";
                req_no = getQryTransID();
                XmlTextWriter xmlWriter = new XmlTextWriter();
                // 写入通用的头部节点
                writeCommonHeader2(xmlWriter);

                XElement tmpXElement = new XElement("body", "");
                tmpXElement.addChild(new XElement("BANK_ID", ""));
                tmpXElement.addChild(new XElement("TRIARG_ID", ""));
                tmpXElement.addChild(new XElement("CUST_ID_NO",StringHelper.isNullOrEmpty(recInfo.DealerCode)?getBankConfig().getValueAsString("custidno"):""));
                tmpXElement.addChild(new XElement("ID_TYPE",StringHelper.isNullOrEmpty(recInfo.DealerCode) ? getBankConfig().getValueAsString("idtype"):""));
                tmpXElement.addChild(new XElement("ERP_CUST_ID",recInfo.DealerCode));
                //主机厂证件号码
                tmpXElement.addChild(new XElement("SV_CUST_ID_NO", getBankConfig().getValueAsString("svcustidno")));
                //主机厂证件类型
                tmpXElement.addChild(new XElement("SV_ID_TYPE", getBankConfig().getValueAsString("svidtype")));
                tmpXElement.addChild(new XElement("CAR_STATUS",""));
                tmpXElement.addChild(new XElement("BACKUP_FIELD", ""));
                tmpXElement.addChild(new XElement("START_INDEX", String.valueOf(index)));
                tmpXElement.addChild(new XElement("PAGE_SIZE", getBankConfig().getValueAsString("pageSize")));
                xmlWriter.writeXElement(tmpXElement);
                xmlWriter.writeEndDocument();
                WriteBankLogStep2(xmlWriter.toXmlString());

                XmlTextWriter xmlWriters = new XmlTextWriter();// 发银行报文的XML对象
                tr_code = "FHTS01";
                writeCommonHeader(xmlWriters);
                xmlWriters.writeStartElement("body");
                xmlWriters.writeElementClosed("rem");
                xmlWriters.writeElementString("systemId", "SCF");
                xmlWriters.writeElementString("data", Base64.encode(xmlWriter.toXmlString().getBytes(getCharset())));
                xmlWriters.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriters.toXmlString());

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    // 解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String ans_code = tmpBkReader.getFirstText("/ap/head/ans_code");
                        String ans_info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if ("0".equals(ans_code)) {
                            String particular_code = tmpBkReader.getFirstText("/ap/head/particular_code");
                            String particular_info = tmpBkReader.getFirstText("/ap/head/particular_info");
                            if ("0000".equals(particular_code)) {
                                String body = new String(Base64.decode(tmpBkReader.getFirstText("/ap/body/RspMsgContent")));
                                WriteBankLogStep3(body);
                                StringBuilder bodyMsg = new StringBuilder();
                                // 解析银行返回
                                XmlTextReader bodyBkReader = loadBankRespXmlReader(body, bodyMsg);
                                if (bodyBkReader != null) {
                                    String rsp_status = bodyBkReader.getFirstText("/mes/head/RSP_STATUS");
                                    String rsp_code = bodyBkReader.getFirstText("/mes/head/RSP_CODE");
                                    String rsp_msg = bodyBkReader.getFirstText("/mes/head/RSP_MSG");
                                    if ("N".equals(rsp_status)) {
                                        String end_flag = bodyBkReader.getFirstText("/mes/body/END_FLAG");
                                        if ("0".equals(end_flag)) {
                                            page = "0";
                                        } else {
                                            page = "1";
                                            tmpRespCode = "0";
                                            tmpRespInfo = rsp_msg;
                                        }
                                        String dealerCode = bodyBkReader.getFirstText("/mes/body/ERP_CUST_ID");
                                        while (bodyBkReader.read("/mes/body/CERTIFICATION_LIST/CERTIFICATION")) {
                                            RetAtsInCustodyInfoDetail tmpRetDetail = new RetAtsInCustodyInfoDetail();
                                            tmpRetDetail.DealerCode = dealerCode;
                                            tmpRetDetail.VIN = bodyBkReader.readedXElement.getSingleText("VIN");
                                            tmpRetDetail.PickupPrice = bodyBkReader.readedXElement.getSingleText("CAR_STOCK_PRICE");
                                            tmpRetDetail.CarMortgageTime = bodyBkReader.readedXElement.getSingleText("IN_STORE_DATE");
                                            tmpRetDetail.CarEneDate = bodyBkReader.readedXElement.getSingleText("CAR_END_DATE");
                                            tmpRetDetail.BillNo = "";
                                            tmpRetDetail.MainRepositoryLocation = bodyBkReader.readedXElement.getSingleText("MAIN_REPOSITORY_LOCATION");
                                            tmpRetDetail.SecondRepositoryLocation = bodyBkReader.readedXElement.getSingleText("SECOND_REPOSITORY_LOCATION");
                                            tmpRetDetail.SecondNetLocation = bodyBkReader.readedXElement.getSingleText("SECOND_NET_LOCATION");
                                            tmpRetDetail.ProduceDate = bodyBkReader.readedXElement.getSingleText("PRODUCE_DATE");
                                            tmpRetDetail.InStoreDate = bodyBkReader.readedXElement.getSingleText("IN_STORE_DATE");
                                            tmpRetDetail.OverHouseDate = bodyBkReader.readedXElement.getSingleText("OVER_HOUSE_DATE");
                                            tmpRetDetail.CarStatus = bodyBkReader.readedXElement.getSingleText("CAR_STATUS");
                                            tmpRetDetail.DiscoveryMoveDate = bodyBkReader.readedXElement.getSingleText("DISCOVERY_MOVE_DATE");
                                            tmpRetDetail.MovePlace = bodyBkReader.readedXElement.getSingleText("MOVE_PLACE");
                                            tmpRetDetail.SaleDate = bodyBkReader.readedXElement.getSingleText("SALE_DATE");
                                            tmpRetDetail.SalePlace = bodyBkReader.readedXElement.getSingleText("SALE_PLACE");
                                            tmpRetDetail.Reserve1 = "";
                                            tmpRetDetail.Reserve2 = "";
                                            tmpRetDetail.Reserve3 = "";
                                            tmpRetDetail.Reserve4 = "";
                                            tmpRetDetail.Reserve5 = "";
                                            retAts.writeRetDetail(tmpRetDetail);
                                            index++;
                                        }
                                    } else {
                                    	tmpRespCode = "-1";
                                        tmpRespInfo = rsp_code + rsp_msg;
                                        break;
                                    }
                                } else {
                                	tmpRespCode = "-1";
                                    tmpRespInfo = "返回报文为空！";
                                    break;
                                }
                            } else {
                            	tmpRespCode = "-1";
                                tmpRespInfo = particular_info;
                                break;
                            }
                        } else {
                        	tmpRespCode = "-1";
                            tmpRespInfo = ans_info;
                            break;
                        }
                    } else {
                    	tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                        break;
                    }
                } else {
                	tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                    break;
                }
            }
            WriteBankLogLn2("获取在押车辆信息条数为：" + (index) + "条！");
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
        }
		retAts.writeRespCode(tmpRespCode);
		retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}

